import os
from openai import OpenAI
import pandas as pd
import concurrent.futures
import threading
import json
import time
import pickle

# 1. 读取图片url
csv_file = 'goods_image_filtered_new_sampled.csv'
df = pd.read_csv(csv_file)
urls = df['image_url'].tolist()
n = 20
chunks = [urls[i::n] for i in range(n)]  # 分成20份

# 2. 初始化模型client
client = OpenAI(
    # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key="sk-xxx",
    api_key='sk-83de4bb24e5e489d976bb94fb834f9cc',
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

q = """
            # Task: Fashion Item Attribute Extractor and Title Generator

            You are an offline, vision-only fashion item analysis expert. Your task is twofold:
            1.  **Attribute Extraction**: Strictly based on the image content, extract the attributes of the main fashion item shown.
            2.  **Title Generation**: Based on the extracted attributes, generate a concise and appealing product title for the item.

            ## Core Rules:

            1.  **Offline Only:** Your analysis must be based **solely** on the provided image. Do not use or reference any external knowledge, brand information, or web search results.
            2.  **Visual Facts First:** Describe only objective, clearly visible facts from the image.
            3.  **Leave Blank if Uncertain:** If an attribute cannot be clearly determined from the image (e.g., the material is unclear, or a logo is unrecognizable), leave the corresponding value empty, i.e., `""`. Do not guess or add notes like '(inferred)'.
            4.  **Output Format:** The output must be a pure JSON list `[]`. Each object `{}` within the list represents a single item. Do not return any other text, explanations, or code block markers like \`\`\`json.

            ## JSON Structure and Field Descriptions:

            -   `product_title`: **(Required)** A compelling and descriptive product title generated by combining key attributes like `style_tags`, `primary_color`, `graphic_description`, and `item_type`.
            -   `item_category`: **(Required)** The general category of the item. Choose from: ["Tops", "Bottoms", "Outerwear", "Dresses & Jumpsuits", "Footwear", "Bags", "Watches", "Jewelry", "Eyewear", "Hats", "Other Accessories"].
            -   `item_type`: **(Required)** The specific type of the item, e.g., "T-Shirt", "Sneakers", "Wristwatch", "Necklace", "Sunglasses".
            -   `primary_color`: **(Required)** The main color of the item.
            -   `secondary_colors`: A list of strings for other significant colors, e.g., `["Black", "White"]`. Use an empty list `[]` if none.
            -   `pattern_type`: The type of pattern. Choose from: ["Solid", "Print", "Striped", "Plaid", "Polka Dot", "Geometric", "Camouflage", "Animal Print", "Other"].
            -   `graphic_description`: A detailed description of any prints, logos, or text. Leave as `""` if none.
            -   `material_texture`: The visually perceived texture of the material, e.g., "Smooth", "Knit Texture", "Denim Weave", "Leather Sheen", "Metallic".
            -   `design_details`: A list of strings for notable design features, e.g., `["Crew Neck", "Short Sleeves", "Vintage Crown Graphic"]` or `["High-Top", "White Laces", "Platform Sole"]`.
            -   `style_tags`: A list of style tags inferred from visual cues, e.g., `["Casual", "Vintage", "Streetwear"]`.

            Expected Output:
            [
                {
                    "product_title": "Vintage Pop Culture DILLY DILLY Crown Graphic Heather Red T-Shirt",
                    "item_category": "Tops",
                    "item_type": "T-Shirt",
                    "primary_color": "Heather Red",
                    "secondary_colors": [
                        "Black"
                    ],
                    "pattern_type": "Print",
                    "graphic_description": "Features a black vintage-style crown and the text 'DILLY DILLY' on the chest",
                    "material_texture": "Knit Texture",
                    "design_details": [
                        "Crew Neck",
                        "Short Sleeves"
                    ],
                    "style_tags": [
                        "Casual",
                        "Vintage",
                        "Pop Culture"
                    ]
                }
            ]
    """

# 线程安全的结果字典
result_dict = {}
lock = threading.Lock()

def process_url(url):
    try:
        completion = client.chat.completions.create(
            model="qwen-vl-max",
            messages=[{"role": "user", "content": [
                {"type": "image_url", "image_url": {"url": url}},
                {"type": "text", "text": q},
            ]}]
        )
        s = completion.choices[0].message.content
        s = s.strip()
        if s.startswith('```json'):
            s = s[7:]
        if s.endswith('```'):
            s = s[:-3]
        s = s.strip()
        value = json.loads(s)
        with lock:
            result_dict[url] = value
    except Exception as e:
        with lock:
            result_dict[url] = {"error": str(e)}
        print(f"Error processing {url}: {e}")

def worker(url_list, thread_id):
    total = len(url_list)
    for idx, url in enumerate(url_list, 1):
        process_url(url)
        print(f"线程{thread_id}: 已处理 {idx}/{total}")
        time.sleep(0.1)  # 可适当加延时防止QPS过高

# 3. 多线程并发处理
with concurrent.futures.ThreadPoolExecutor(max_workers=n) as executor:
    futures = [executor.submit(worker, chunk, i) for i, chunk in enumerate(chunks, 1)]
    concurrent.futures.wait(futures)

# 4. 结果保存
with open('image_url_to_model_output.pkl', 'wb') as f:
    pickle.dump(result_dict, f)
print("全部处理完成，结果已保存为 image_url_to_model_output.pkl")